{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Execute this cell to install dependencies\n",
    "%pip install sf-hamilton[visualization] pandas scikit-learn numpy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Procedural MPG Notebook [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/dagworks-inc/hamilton/blob/main/examples/hamilton-tutorials/mpg-translation/ProceduralMPGNotebook.ipynb) [![GitHub badge](https://img.shields.io/badge/github-view_source-2b3137?logo=github)](https://github.com/apache/hamilton/blob/main/examples/hamilton-tutorials/mpg-translation/ProceduralMPGNotebook.ipynb)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.metrics import mean_absolute_error\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-20T17:40:23.102334Z",
     "start_time": "2024-07-20T17:40:23.034851Z"
    },
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "6e1403f7-5b44-4fdd-89ec-bbab8d026365",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    MPG  Cylinders  Displacement  Horsepower  Weight  Acceleration  ModelYear  Origin\n",
      "0  18.0          8         307.0       130.0  3504.0          12.0         70       1\n",
      "1  15.0          8         350.0       165.0  3693.0          11.5         70       1\n",
      "2  18.0          8         318.0       150.0  3436.0          11.0         70       1\n",
      "3  16.0          8         304.0       150.0  3433.0          12.0         70       1\n",
      "4  17.0          8         302.0       140.0  3449.0          10.5         70       1\n"
     ]
    }
   ],
   "source": [
    "# Load the data\n",
    "url = \"http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data\"\n",
    "column_names = [\n",
    "    \"MPG\",\n",
    "    \"Cylinders\",\n",
    "    \"Displacement\",\n",
    "    \"Horsepower\",\n",
    "    \"Weight\",\n",
    "    \"Acceleration\",\n",
    "    \"Model Year\",\n",
    "    \"Origin\",\n",
    "]\n",
    "\n",
    "raw_dataset = pd.read_csv(\n",
    "    url, names=column_names, na_values=\"?\", comment=\"\\t\", sep=\" \", skipinitialspace=True\n",
    ")\n",
    "\n",
    "# rename column\n",
    "raw_dataset = raw_dataset.rename(columns={\"Model Year\": \"ModelYear\"})\n",
    "print(raw_dataset.head().to_string())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-20T17:40:23.745339Z",
     "start_time": "2024-07-20T17:40:23.736142Z"
    },
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "3275ee06-5c89-4a22-b15d-fe1e6c245fdd",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [],
   "source": [
    "# Do some feature engineering / data cleaning to create the data sets\n",
    "# one hot encode -- we know the encoding here.\n",
    "for value, country in {1: \"USA\", 2: \"Europe\", 3: \"Japan\"}.items():\n",
    "    raw_dataset[country] = np.where(raw_dataset[\"Origin\"] == value, 1, 0)\n",
    "raw_dataset = raw_dataset.dropna()\n",
    "# create data sets\n",
    "train_test_split = 0.8\n",
    "seed = 123\n",
    "# split the pandas dataframe into train and test\n",
    "train_dataset = raw_dataset.sample(frac=train_test_split, random_state=seed)\n",
    "test_dataset = raw_dataset.drop(train_dataset.index)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-20T17:40:24.616240Z",
     "start_time": "2024-07-20T17:40:24.599947Z"
    },
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "4b3acb6a-3d1c-4197-8db3-788846c93e22",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-1 {color: black;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>LinearRegression()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">LinearRegression</label><div class=\"sk-toggleable__content\"><pre>LinearRegression()</pre></div></div></div></div></div>"
      ],
      "text/plain": [
       "LinearRegression()"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# config for fitting a model\n",
    "target_column: str = \"MPG\"\n",
    "\n",
    "# pull out target\n",
    "train_labels = train_dataset.pop(target_column)\n",
    "\n",
    "# Convert boolean columns to integers for the model\n",
    "bool_columns = train_dataset.select_dtypes(include=[bool]).columns\n",
    "train_dataset[bool_columns] = train_dataset[bool_columns].astype(int)\n",
    "\n",
    "# Normalize the features for the model\n",
    "scaler = StandardScaler()\n",
    "train_dataset_scaled = scaler.fit_transform(train_dataset)\n",
    "\n",
    "# Initialize and fit the Linear Regression model\n",
    "linear_model = LinearRegression()\n",
    "linear_model.fit(train_dataset_scaled, train_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-20T17:40:44.362226Z",
     "start_time": "2024-07-20T17:40:44.354827Z"
    },
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "5c9ce363-bf45-492c-a12d-9d0be8c7f05a",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [],
   "source": [
    "# evaluate the model - pull out target\n",
    "test_labels = test_dataset.pop(target_column)\n",
    "\n",
    "# convert boolean columns to integers for the model\n",
    "bool_columns = test_dataset.select_dtypes(include=[bool]).columns\n",
    "test_dataset[bool_columns] = test_dataset[bool_columns].astype(int)\n",
    "test_dataset_scaled = scaler.transform(test_dataset)\n",
    "\n",
    "# Predict and evaluate the model\n",
    "test_pred = linear_model.predict(test_dataset_scaled)\n",
    "mae = mean_absolute_error(test_labels, test_pred)\n",
    "test_results = {\n",
    "    \"linear_model\": mae\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-20T17:40:45.139892Z",
     "start_time": "2024-07-20T17:40:45.135780Z"
    },
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {
      "byteLimit": 2048000,
      "rowLimit": 10000
     },
     "inputWidgets": {},
     "nuid": "22aff86e-0897-4f4b-99f5-e3b1d8cbd1a3",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'linear_model': 2.4926580150007007} LinearRegression()\n"
     ]
    }
   ],
   "source": [
    "print(test_results, linear_model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "application/vnd.databricks.v1+cell": {
     "cellMetadata": {},
     "inputWidgets": {},
     "nuid": "bfe3201c-dc6b-40e0-92c2-f3a2480f8e7b",
     "showTitle": false,
     "title": ""
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "application/vnd.databricks.v1+notebook": {
   "dashboards": [],
   "language": "python",
   "notebookMetadata": {
    "pythonIndentUnit": 4
   },
   "notebookName": "Procedural MPG Notebook",
   "widgets": {}
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
